package Servlet.Admin;

import Entity.MyTableData;

import Service.Admin.Imp.StageTimeServiceImp;
import Service.Admin.StageTimeService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/queryAllStageTime", "/updateStageTime", "/getStageTimeBystage"})
public class StageTimeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/queryAllStageTime".equals(url)) {
            queryAllStageTime(request, response);
        } else if ("/updateStageTime".equals(url)) {
            updateStageTime(request, response);
        } else if ("/getStageTimeBystage".equals(url)) {
            updateStageTime(request, response);
        }
    }

    /**
     * 得到所有阶段时间信息
     */
    protected void queryAllStageTime(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StageTimeService stageTimeService = new StageTimeServiceImp();
        MyTableData myTableData = stageTimeService.queryAllStageTime();
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 按阶段编号查询阶段时间信息
     */
    private void getStageTimeBystage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StageTimeService stageTimeService = new StageTimeServiceImp();
        String stageId = request.getParameter("stageId");
        MyTableData myTableData = stageTimeService.getStageTimeBystage(stageId);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 编辑阶段时间信息
     */
    protected void updateStageTime(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StageTimeService stageTimeService = new StageTimeServiceImp();
//        String taskData= request.getParameter("taskData");
//        System.out.println(taskData);

        String startTime = request.getParameter("startTime");
//        System.out.println(startTime.length());
//        System.out.println("2021-08-21".length());
        String endTime = request.getParameter("endTime");
//        System.out.println(endTime);
        String stageId = request.getParameter("stageId");
//        System.out.println(stageId);
        boolean flag = stageTimeService.updateStageTime(startTime, endTime, stageId);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
